//
//  ViewController.m
//  UIViewTransformPoint
//
//  Created by codew on 4/30/21.
//

#import "ViewController.h"


@interface ViewController ()



@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self test5];
    
//    dispatch_queue_t que = dispatch_queue_create("com.itopengl", DISPATCH_QUEUE_CONCURRENT);
//
//    dispatch_async(que, ^{
//
//        [self test4];
//    });
}



- (void)test1
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    
    
    dispatch_group_t dispatchGroup = dispatch_group_create();
        
    for (int i = 0; i < 2; i++) {
        
                
        dispatch_group_enter(dispatchGroup);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSLog(@"%@", [NSThread currentThread]);
            
            NSURLSession *session = [NSURLSession sharedSession];
            
            NSURL *url = [NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"];
            
            
            NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                            downloadTaskWithURL:url
                                                            completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                
                
                NSLog(@"ok ok, %d", i);
                
                dispatch_group_leave(dispatchGroup);
                
                
                
            }];
            
            [sessionDownloadTask resume];
            
        });
        
    }
    
        
    
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀.");
    });
}



- (void)test2
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    
    NSURLSession *session = [NSURLSession sharedSession];
    dispatch_group_t dispatchGroup = dispatch_group_create();
    
    
    dispatch_group_enter(dispatchGroup);
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"got data from internet%@", [NSThread currentThread]);
        dispatch_group_leave(dispatchGroup);
    }];
    [task resume];
    
    
    dispatch_group_enter(dispatchGroup);
    NSURLSessionDataTask *task2 = [session dataTaskWithURL:[NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"got data from internet2%@", [NSThread currentThread]);
        dispatch_group_leave(dispatchGroup);
    }];
    [task2 resume];
    
    
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end");
    });
}


- (void)test3
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    
    NSURLSession *session = [NSURLSession sharedSession];
    dispatch_group_t dispatchGroup = dispatch_group_create();
    
    
    dispatch_group_enter(dispatchGroup);
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"got data from internet1");
    
        NSLog(@"ok ok 1%@", [NSThread currentThread]);
        dispatch_group_leave(dispatchGroup);
    }];
    [task resume];
    
    
    
    dispatch_group_enter(dispatchGroup);
    NSURLSessionDataTask *task2 = [session dataTaskWithURL:[NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"got data from internet2");
        
        NSLog(@"ok ok 2%@", [NSThread currentThread]);
        dispatch_group_leave(dispatchGroup);
    }];
    [task2 resume];
    
    
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end");
    });
}

/** 错误演示 */

- (void)test4
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    
    
    dispatch_group_t dispatchGroup = dispatch_group_create();
        
    for (int i = 0; i < 2; i++) {
        
                
        
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            
            NSLog(@"%@", [NSThread currentThread]);
            dispatch_group_enter(dispatchGroup);
            NSURLSession *session = [NSURLSession sharedSession];
            
            NSURL *url = [NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"];
            
            
            NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                            downloadTaskWithURL:url
                                                            completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                
                
                NSLog(@"ok ok, %d", i);
                
                dispatch_group_leave(dispatchGroup);
                
                
                
            }];
            
            [sessionDownloadTask resume];
            
        });
        
    }
    
        
    
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀.");
    });
}


- (void)test5
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    dispatch_group_t dispatchGroup = dispatch_group_create();
        
    for (int i = 0; i < 6; i++) {
                        
        dispatch_group_enter(dispatchGroup);
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"];
                
        NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                        downloadTaskWithURL:url
                                                        completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                    
            location;
            NSLog(@"%@, ok ok, %d", [NSThread currentThread],i);
            dispatch_group_leave(dispatchGroup);
        }];
        
        
        [sessionDownloadTask resume];
    }

            
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀.");
    });
}


/** 错误演示 */
- (void)test6
{
    
    NSLog(@"%s, %@", __func__, [NSThread currentThread]);
    dispatch_group_t dispatchGroup = dispatch_group_create();
    dispatch_queue_t que = dispatch_queue_create("com.itopengl", DISPATCH_QUEUE_CONCURRENT);
        
    for (int i = 0; i < 2; i++) {
                        
        
        dispatch_group_async(dispatchGroup, que, ^{
            
            NSURLSession *session = [NSURLSession sharedSession];
            NSURL *url = [NSURL URLWithString:@"https://cdn.jsdelivr.net/gh/coderTong/codercdn/mianm/mmaudio/20210508/iOS%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96%E6%96%B9%E6%A1%88%E6%9C%89%E5%93%AA%E4%BA%9B.m4a"];
                    
            NSURLSessionDownloadTask *sessionDownloadTask =[session
                                                            downloadTaskWithURL:url
                                                            completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                        
                NSLog(@"%@, ok ok, %d", [NSThread currentThread],i);
                
            }];
            
            
            [sessionDownloadTask resume];
            
        });
        
        
        
    }

            
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
        NSLog(@"end 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀.");
    });
}
@end
